<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="豆瓣URL" prop="url">
      <el-input v-model="dataForm.url" placeholder="豆瓣URL"></el-input>
    </el-form-item>
    <el-form-item label="简介" prop="perms">
      <el-input v-model="dataForm.perms" placeholder="简介"></el-input>
    </el-form-item>
    <el-form-item label="封面URL" prop="imageurl">
      <el-input v-model="dataForm.imageurl" placeholder="封面URL"></el-input>
    </el-form-item>
    <el-form-item label="评分" prop="fenshu">
      <el-input v-model="dataForm.fenshu" placeholder="评分"></el-input>
    </el-form-item>
    <el-form-item label="上映日期" prop="releaseDate">
      <el-input v-model="dataForm.releaseDate" placeholder="上映日期"></el-input>
    </el-form-item>
    <el-form-item label="腾讯视频播放地址" prop="txurl">
      <el-input v-model="dataForm.txurl" placeholder="腾讯视频播放地址"></el-input>
    </el-form-item>
    <el-form-item label="优酷播放地址" prop="youkuurl">
      <el-input v-model="dataForm.youkuurl" placeholder="优酷播放地址"></el-input>
    </el-form-item>
    <el-form-item label="爱奇艺播放地址" prop="aqiyiurl">
      <el-input v-model="dataForm.aqiyiurl" placeholder="爱奇艺播放地址"></el-input>
    </el-form-item>
    <el-form-item label="芒果播放地址" prop="mangguourl">
      <el-input v-model="dataForm.mangguourl" placeholder="芒果播放地址"></el-input>
    </el-form-item>
    <el-form-item label="乐视播放地址" prop="leshiurl">
      <el-input v-model="dataForm.leshiurl" placeholder="乐视播放地址"></el-input>
    </el-form-item>
    <el-form-item label="状态  0：禁用   1：正常" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态  0：禁用   1：正常"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifyTime">
      <el-input v-model="dataForm.modifyTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          url: '',
          perms: '',
          imageurl: '',
          fenshu: '',
          releaseDate: '',
          txurl: '',
          youkuurl: '',
          aqiyiurl: '',
          mangguourl: '',
          leshiurl: '',
          status: '',
          createTime: '',
          modifyTime: ''
        },
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '豆瓣URL不能为空', trigger: 'blur' }
          ],
          perms: [
            { required: true, message: '简介不能为空', trigger: 'blur' }
          ],
          imageurl: [
            { required: true, message: '封面URL不能为空', trigger: 'blur' }
          ],
          fenshu: [
            { required: true, message: '评分不能为空', trigger: 'blur' }
          ],
          releaseDate: [
            { required: true, message: '上映日期不能为空', trigger: 'blur' }
          ],
          txurl: [
            { required: true, message: '腾讯视频播放地址不能为空', trigger: 'blur' }
          ],
          youkuurl: [
            { required: true, message: '优酷播放地址不能为空', trigger: 'blur' }
          ],
          aqiyiurl: [
            { required: true, message: '爱奇艺播放地址不能为空', trigger: 'blur' }
          ],
          mangguourl: [
            { required: true, message: '芒果播放地址不能为空', trigger: 'blur' }
          ],
          leshiurl: [
            { required: true, message: '乐视播放地址不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态  0：禁用   1：正常不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifyTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/movie/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.movie.name
                this.dataForm.url = data.movie.url
                this.dataForm.perms = data.movie.perms
                this.dataForm.imageurl = data.movie.imageurl
                this.dataForm.fenshu = data.movie.fenshu
                this.dataForm.releaseDate = data.movie.releaseDate
                this.dataForm.txurl = data.movie.txurl
                this.dataForm.youkuurl = data.movie.youkuurl
                this.dataForm.aqiyiurl = data.movie.aqiyiurl
                this.dataForm.mangguourl = data.movie.mangguourl
                this.dataForm.leshiurl = data.movie.leshiurl
                this.dataForm.status = data.movie.status
                this.dataForm.createTime = data.movie.createTime
                this.dataForm.modifyTime = data.movie.modifyTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/movie/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'url': this.dataForm.url,
                'perms': this.dataForm.perms,
                'imageurl': this.dataForm.imageurl,
                'fenshu': this.dataForm.fenshu,
                'releaseDate': this.dataForm.releaseDate,
                'txurl': this.dataForm.txurl,
                'youkuurl': this.dataForm.youkuurl,
                'aqiyiurl': this.dataForm.aqiyiurl,
                'mangguourl': this.dataForm.mangguourl,
                'leshiurl': this.dataForm.leshiurl,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'modifyTime': this.dataForm.modifyTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
